package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DisFlavorMapper;
import com.sky.mapper.dishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.apache.el.stream.Stream;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServicelmpl implements DishService {

    @Autowired
    private dishMapper dishMapper;
    @Autowired
    private DisFlavorMapper dishFlavorMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        //拷贝dto对象进入实体对象
        BeanUtils.copyProperties(dishDTO, dish);
        //先添加菜品
        //dish.setStatus(StatusConstant.DISABLE);//默认设置为禁售
        dishMapper.insertDish(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            //将菜品的口味添加进入口味-菜品表
            flavors.forEach(flavor -> {
                //循环插入口味表
                flavor.setDishId(dish.getId());//返回插入菜品的id
                dishFlavorMapper.add(flavor);
            });

        }

    }


    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> select = dishMapper.select(dishPageQueryDTO);
        long l = select.getTotal();
        List<DishVO> result = select.getResult();
        PageResult pageResult = new PageResult(l, result);
        return pageResult;
    }


    @Override
    //todo 后续的修改没写
    public DishVO select(int i) {
        //返回菜品信息
        DishVO dishVO = dishMapper.selectById(i);
        //返回菜品口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(i);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDish(DishDTO dishDTO) {
        //修改菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        //修改口味表
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && !flavors.isEmpty()) {/**/
            //将菜品id写入口味列表
            Long id = dish.getId();
            flavors.forEach(flavor -> {
                flavor.setDishId(id);
            });
            //修改的菜品有新口味写入
            /*//查看该菜品id是否有口味记录
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(id.intValue());
            if (dishFlavors != null && !dishFlavors.isEmpty()) {*/
            //口味表有数据，需要先删除，在重新将修改的写入
            dishFlavorMapper.delect(id);
            dishFlavorMapper.insert(flavors);
            /*}*/
            //没有记录，直接插入
            //dishFlavorMapper.insert(flavors);
        } else {
            //没有口味写入，就查看原口味表是否有口味，将其删除
           /* List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(id.intValue());
            if (dishFlavors != null && !dishFlavors.isEmpty()) {
                //口味表有数据，需要先删除，在重新将修改的写入*/
            dishFlavorMapper.delect(dish.getId());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(String ids) {
        ArrayList<Integer> collect = (ArrayList<Integer>) Arrays.stream(ids.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        //先删除菜品表
        dishMapper.delete(collect);
        //在删除口味表
        collect.forEach(i ->dishFlavorMapper.delect(i.longValue()));

    }

    @Override
    public void status(Integer id, Integer status) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id.longValue());
        dishMapper.set(dish);
    }

}

